package com.wss.common.base;

import butterknife.BindComponent;
import cn.bingoogolapple.refreshlayout.BgaStickinessRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BgarefreshLayout;
import com.wss.common.base.mvp.BasePresenter;
import ohos.aafwk.content.Intent;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.ListContainer;

public abstract class BaseRefreshListAbility<P extends BasePresenter> extends BaseActionBarAbility<P> {

//    @BindComponent(ResourceTable.Id_ptrl_list)
    BgarefreshLayout refreshLayout;

//    @BindComponent(ResourceTable.Id_recycle_view)
    ListContainer recyclerView;

    /**
     * 适配器
     */
    private BaseItemProvider adapter;

    private boolean isFirstIn = true;

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_layout_refresh;
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
    }

    @Override
    protected void onActive() {
        super.onActive();
        if (isFirstIn) {
            isFirstIn = false;
            initView();
        }
    }

    protected void initView() {

        refreshLayout = (BgarefreshLayout) findComponentById(ResourceTable.Id_ptrl_list);
        recyclerView = (ListContainer) findComponentById(ResourceTable.Id_recycle_view);

        adapter = createAdapter();
        refreshLayout.setDelegate(createRefreshListener());
        recyclerView.setItemProvider(adapter);

        initRefresh();
    }

    private void initRefresh() {

        BgaStickinessRefreshViewHolder stickinessRefreshViewHolder = new BgaStickinessRefreshViewHolder(this, true);
        refreshLayout.setRefreshViewHolder(stickinessRefreshViewHolder);
        refreshLayout.setIsShowLoadingMoreView(true);
        refreshLayout.setPullDownRefreshEnable(true);
        stickinessRefreshViewHolder.setPullDownRefreshText("释放立即刷新...");
        stickinessRefreshViewHolder.setLoadingMoreText("加载更多...");
        stickinessRefreshViewHolder.setUltimateColor(ResourceTable.Color_gray);
        stickinessRefreshViewHolder.setLoadMoreBackgroundColorRes(ResourceTable.Color_light_white);
        stickinessRefreshViewHolder.setRefreshViewBackgroundColorRes(ResourceTable.Color_light_white);
        stickinessRefreshViewHolder.setLoaderTextColor(ResourceTable.Color_gray);
        stickinessRefreshViewHolder.setPullUpDilogueVisible(false);
        stickinessRefreshViewHolder.setLoaderTextSize(40);
        stickinessRefreshViewHolder.setProgressSize(80);
    }

    @Override
    public void dismissLoading() {
        super.dismissLoading();
        stopRefresh();
    }


    @Override
    public void showLoading() {
        super.showLoading();
        hideEmptyView();
    }

    /**
     * 停止刷新
     */
    protected void stopRefresh() {
        refreshLayout.endRefreshing();
        refreshLayout.endLoadingMore();
    }

    /**
     * 获取刷新控件
     *
     * @return PullToRefreshLayout
     */
    protected BgarefreshLayout getRefreshLayout() {
        return refreshLayout;
    }

    /**
     * 获取加载数据列表控件
     *
     * @return RecyclerView
     */
    protected ListContainer getRecyclerView() {
        return recyclerView;
    }

    /**
     * 返回适配器
     *
     * @return BaseItemProvider
     */
    protected BaseItemProvider getAdapter() {
        return adapter;
    }


    /**
     * 创建刷新监听器
     *
     * @return BgarefreshLayout.BgarefreshLayoutDelegate
     */
    protected abstract BgarefreshLayout.BgarefreshLayoutDelegate createRefreshListener();

    /**
     * 创建列表适配器
     *
     * @return BaseItemProvider
     */
    protected abstract BaseItemProvider createAdapter();
}
